{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fcca2bdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f274dd20",
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建人脸库（通过个人APIKEY）"
   ]
  },
  {
   "cell_type": "raw",
   "id": "102081c6",
   "metadata": {},
   "source": [
    "API_KEY = 'vGlwjQJXYKWyXOELKQFxrFFQzD88d4Cx'\n",
    "API_S = 'rKDegTysiZpO47UF-Y_-bMwxN8PygYEN'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'display_name':'测试用人脸库'\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b25b585e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "97cbc0ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466266,9759160d-f5de-4abc-9921-eeff339ec0e4',\n",
       " 'time_used': 311,\n",
       " 'faces': [{'face_token': '898601eea5442d75ed6554e9bee9e5a5',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67f2806d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查询人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d6a5a99a",
   "metadata": {},
   "outputs": [],
   "source": [
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "936632e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 95,\n",
       " 'facesets': [{'faceset_token': '3c72f3b8a330e4b286eade4251a76812',\n",
       "   'outer_id': '',\n",
       "   'display_name': '测试用人脸库',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648465323,2f288b14-a3ac-406e-9acd-e488168adfaf'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "77be5613",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将人脸库特征码存入faceset_tokens变量中\n",
    "faceset_tokens = r.json()['facesets'][-1]['faceset_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec47b1d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将人脸特征码存入人脸库\n",
    "\n",
    "##首先需要获得想要放入的人脸特征码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12372565",
   "metadata": {},
   "outputs": [],
   "source": [
    "#通过导入外部文件调用自定义模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "28a8effa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a5de642a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\27317\\desktop\\人工智能测试\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e4fcd53e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用传统方法获取人脸特征码\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "93d9270c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648468786,7bc8d3ec-335d-4676-a0fe-c332c1c6fea3',\n",
       " 'time_used': 490,\n",
       " 'faces': [{'face_token': '6fd6ee5a4419679add2d1161f7320a0b',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c64fc74b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将获取到的特征码放到face_tokens1变量\n",
    "\n",
    "face_tokens1 = r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d489dcb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用另外一个API：addface将特征码添加进人脸库\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_tokens,\n",
    "    'face_tokens':face_tokens1 \n",
    "}\n",
    "\n",
    "r = requests.post(FaceSet_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "607d2604",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用新API利用人脸库特征码来查询已有的人脸库\n",
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_tokens\n",
    "}\n",
    "r = requests.post(url = getdetail_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1eab0781",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3c72f3b8a330e4b286eade4251a76812',\n",
       " 'tags': '',\n",
       " 'time_used': 92,\n",
       " 'user_data': '',\n",
       " 'display_name': '测试用人脸库',\n",
       " 'face_tokens': ['898601eea5442d75ed6554e9bee9e5a5'],\n",
       " 'face_count': 1,\n",
       " 'request_id': '1648467333,e0324388-7b53-4c95-997b-6c3c20f1e02e',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "228fba1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用新API为已有的人脸库更新信息\n",
    "update_FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_tokens,\n",
    "    'new_outer_id':'south9',\n",
    "    'display_name':'西九413',\n",
    "    'user_data':'4人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url = update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "cbc8211c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3c72f3b8a330e4b286eade4251a76812',\n",
       " 'request_id': '1648467646,93de9c0b-be54-4268-9f4f-5c702fb915d5',\n",
       " 'time_used': 71,\n",
       " 'outer_id': 'south9'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "db47751e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#通过新API查询该APIKEY所创建的所有人脸库\n",
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b44e566c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 75,\n",
       " 'facesets': [{'faceset_token': '3c72f3b8a330e4b286eade4251a76812',\n",
       "   'outer_id': 'south9',\n",
       "   'display_name': '西九413',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648467737,7fbca609-272b-4b6d-95d9-8a01a65176ea'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过查询了解该APIKEY创建了一个人脸库\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1514b567",
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业1：创建analyze.py外部文件并调用\n",
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "cecca927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, face_tokens1)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\27317\\desktop\\人工智能测试\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "893c2597",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 51,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1648468795,b293925d-d71e-4e65-a494-c9a270a4295e'}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法命名有误，后续若有BUG注意修改\n",
    "analyze.face_detect(API_KEY,API_S,face_tokens1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3d103f89",
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业2：通过search API查询某人脸是否存在于人脸库中\n",
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e1e73fc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_sercret, face_tokens1, faceset_token1)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\27317\\desktop\\人工智能测试\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "31b1dea2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648469595,643c39e8-1148-4ced-bd3d-b308fac4e9d7',\n",
       " 'time_used': 322,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': '898601eea5442d75ed6554e9bee9e5a5'}]}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_KEY,API_S,face_tokens1,faceset_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "664286b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "此人脸存在于人脸库\n"
     ]
    }
   ],
   "source": [
    "result = search.face_search(API_KEY,API_S,face_tokens1,faceset_tokens)['results'][0]['confidence']\n",
    "if result > 90:\n",
    "    print('此人脸存在于人脸库')\n",
    "else:\n",
    "    print('此人脸不存在于人脸库')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f499f42",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
